<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否热销商品" prop="isHot">
        <el-select v-model="queryParams.isHot" placeholder="请选择是否热销商品" clearable size="small">
          <el-option
            v-for="dict in isHotOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否是精品" prop="isBest">
        <el-select v-model="queryParams.isBest" placeholder="请选择是否是精品" clearable size="small">
          <el-option
            v-for="dict in isBestOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否上架" prop="isOnSale">
        <el-select v-model="queryParams.isOnSale" placeholder="请选择是否上架" clearable size="small">
          <el-option
            v-for="dict in isOnSaleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsSn">
        <el-input
          v-model="queryParams.goodsSn"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplierGoods:supplierGoods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplierGoods:supplierGoods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplierGoods:supplierGoods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplierGoods:supplierGoods:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="supplierGoodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="商品ID" align="center" prop="goodsId" /> -->
      <el-table-column label="商铺ID" align="center" prop="supplierId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品店铺价格" align="center" prop="supplierPrice" />
      <el-table-column label="商品排序" align="center" prop="supplierSort" />
      <el-table-column label="是否热销商品" align="center" prop="isHot" :formatter="isHotFormat" />
      <el-table-column label="是否是新品" align="center" prop="isNew" :formatter="isNewFormat" />
      <el-table-column label="是否是精品" align="center" prop="isBest" :formatter="isBestFormat" />
      <el-table-column label="是否上架" align="center" prop="isOnSale" :formatter="isOnSaleFormat" />
      <el-table-column label="店铺商品浏览量" align="center" prop="clickCount" />
      <el-table-column label="商品编号" align="center" prop="goodsSn" />
      <el-table-column label="系列ID" align="center" prop="seriesId" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplierGoods:supplierGoods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplierGoods:supplierGoods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品ID" prop="goodsId">
              <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商铺ID" prop="supplierId">
              <el-input v-model="form.supplierId" placeholder="请输入商铺ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="商品名称" prop="goodsName">
              <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品店铺价格" prop="supplierPrice">
              <el-input type="number" v-model="form.supplierPrice" placeholder="请输入商品店铺价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品活动价格" prop="promotePrice">
              <el-input type="number" v-model="form.promotePrice" placeholder="请输入商品活动价格" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品活动开始时间" prop="promoteStartTime">
              <el-date-picker clearable size="small" style="width: 200px"
                v-model="form.promoteStartTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择商品活动开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品活动结束时间" prop="promoteEdnTime">
              <el-date-picker clearable size="small" style="width: 200px"
                v-model="form.promoteEdnTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择商品活动结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="商品虚拟销量" prop="virtualSales">
              <el-input type="number" v-model="form.virtualSales" placeholder="请输入商品虚拟销量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品真实销量" prop="salesCount">
              <el-input type="number" v-model="form.salesCount" placeholder="请输入商品真实销量" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否热销商品">
              <el-select v-model="form.isHot" placeholder="请选择是否热销商品">
                <el-option
                  v-for="dict in isHotOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否是新品">
              <el-select v-model="form.isNew" placeholder="请选择是否是新品">
                <el-option
                  v-for="dict in isNewOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否是精品">
              <el-select v-model="form.isBest" placeholder="请选择是否是精品">
                <el-option
                  v-for="dict in isBestOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否上架">
              <el-select v-model="form.isOnSale" placeholder="请选择是否上架">
                <el-option
                  v-for="dict in isOnSaleOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="店铺商品浏览量" prop="clickCount">
              <el-input v-model="form.clickCount" placeholder="请输入店铺商品浏览量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品编号" prop="goodsSn">
              <el-input v-model="form.goodsSn" placeholder="请输入商品编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="商品排序" prop="supplierSort">
              <el-input type="number" v-model="form.supplierSort" placeholder="请输入商品排序" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplierGoods, getSupplierGoods, delSupplierGoods, addSupplierGoods, updateSupplierGoods, exportSupplierGoods } from "@/api/supplierGoods/supplierGoods";

export default {
  name: "SupplierGoods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商家商品表格数据
      supplierGoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      isActiveOptions: [],
      // 是否热销商品字典
      isHotOptions: [],
      // 是否是新品字典
      isNewOptions: [],
      // 是否是精品字典
      isBestOptions: [],
      // 是否上架字典
      isOnSaleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        isHot: undefined,
        isBest: undefined,
        isOnSale: undefined,
        goodsSn: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("is_active").then(response => {
      this.isActiveOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isHotOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isNewOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isBestOptions = response.data;
    });
    this.getDicts("is_active").then(response => {
      this.isOnSaleOptions = response.data;
    });
  },
  methods: {
    /** 查询商家商品列表 */
    getList() {
      this.loading = true;
      listSupplierGoods(this.queryParams).then(response => {
        this.supplierGoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否有效字典翻译
    isActiveFormat(row, column) {
      return this.selectDictLabel(this.isActiveOptions, row.isActive);
    },
    // 是否热销商品字典翻译
    isHotFormat(row, column) {
      return this.selectDictLabel(this.isHotOptions, row.isHot);
    },
    // 是否是新品字典翻译
    isNewFormat(row, column) {
      return this.selectDictLabel(this.isNewOptions, row.isNew);
    },
    // 是否是精品字典翻译
    isBestFormat(row, column) {
      return this.selectDictLabel(this.isBestOptions, row.isBest);
    },
    // 是否上架字典翻译
    isOnSaleFormat(row, column) {
      return this.selectDictLabel(this.isOnSaleOptions, row.isOnSale);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isActive: undefined,
        lesseeId: undefined,
        goodsId: undefined,
        supplierId: undefined,
        goodsName: undefined,
        supplierPrice: undefined,
        promotePrice: undefined,
        promoteStartTime: undefined,
        promoteEdnTime: undefined,
        supplierSort: undefined,
        virtualSales: undefined,
        salesCount: undefined,
        isHot: undefined,
        isNew: undefined,
        isBest: undefined,
        isOnSale: undefined,
        clickCount: undefined,
        goodsSn: undefined,
        seriesId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplierGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家商品";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSupplierGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSupplierGoods(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商家商品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSupplierGoods(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商家商品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSupplierGoods(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
